package com.jvup.gameshow.view.attributeBinder;

import android.view.View;

import androidx.core.widget.NestedScrollView;
import androidx.databinding.BindingAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.AppBarLayout.OnOffsetChangedListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener;
import com.jvup.common.utils.ToastHelper;

/**
 * 原生控件事件扩展绑定适配器
 */
public class ViewEventAdapter {

    public static final long limitTime = 500;

    private static int  lastViewCode = -1;
    private static long lastPressTime = 0;

    public static boolean onIntercept(View view) {
        int viewCode = view.hashCode();
        long currentTime = System.currentTimeMillis();
        long intervalTime = currentTime - lastPressTime;
        if (viewCode == lastViewCode && limitTime > intervalTime) {
            return true;
        }

        lastPressTime = currentTime;
        lastViewCode = viewCode;
        return false;
    }

    @BindingAdapter("android:onClick")
    public static void setOnClickListener(View view, View.OnClickListener listener) {
        view.setOnClickListener(v -> {
            if (onIntercept(v)) {
                ToastHelper.showToast("请不要重复按键~~");
                return;
            }
            if (listener != null) {
                listener.onClick(v);
            }
        });
    }

    @BindingAdapter("onOffsetChanged")
    public static void addOnOffsetChangedListener(AppBarLayout appBarLayout, OnOffsetChangedListener onOffsetChangedListener) {
        appBarLayout.addOnOffsetChangedListener(onOffsetChangedListener);
    }
    @BindingAdapter("onScrollChange")
    public static void setOnScrollChangeListener(NestedScrollView nestedScrollView, NestedScrollView.OnScrollChangeListener onScrollChangeListener) {
        nestedScrollView.setOnScrollChangeListener(onScrollChangeListener);
    }

    @BindingAdapter(value = {"onPageScrolled", "onPageSelected", "onPageScrollStateChanged"}, requireAll = false)
    public static void addOnPageChangeListener(ViewPager viewPager, OnPageScrolled onPageScrolled,
                                               OnPageSelected onPageSelected, OnPageScrollStateChanged onPageScrollStateChanged){
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(onPageScrolled!=null)
                    onPageScrolled.onPageScrolled(position,positionOffset,positionOffsetPixels);
            }
            @Override
            public void onPageSelected(int position) {
                if(onPageSelected!=null)
                    onPageSelected.onPageSelected(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
                if(onPageScrollStateChanged!=null)
                    onPageScrollStateChanged.onPageScrollStateChanged(state);
            }
        });
    }

    public interface OnPageScrolled{
        void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
    }

    public interface OnPageSelected{
        void onPageSelected(int position);
    }

    public interface OnPageScrollStateChanged{
        void onPageScrollStateChanged(int state);
    }

    @BindingAdapter("onNavigationItemSelectedListener")
    public static void setOnNavigationItemSelectedListener(BottomNavigationView view, OnNavigationItemSelectedListener listener) {
        view.setOnNavigationItemSelectedListener(listener);
    }

}
